///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Summit Contributors.  
// See: http://code.google.com/p/tyemill/wiki/SummitContributors
//
// The name and logo of Summit are trademarks of TYEMILL, LLC - All Rights 
// Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, 
// sublicense, and/or sell copies of the Software, and to permit persons to 
// whom the Software is furnished to do so, subject to the following 
// conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////

package summit.basecamp.helpers
{
import summit.basecamp.constants.BasecampOperation;
import summit.basecamp.constants.BasecampResource;
import summit.net.BasecampRequest;
import flash.net.URLRequestMethod;

/**
 * Helpers for Basecamp comment operations.
 */
public class CommentHelper
{
	/** @private Constants **/
	private static const PATH_HELPER:String = "/comments/";
	private static const TYPE:String = BasecampResource.COMMENT;
	
	/** @private **/	
	public function CommentHelper()
	{
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for creating a comment.
	 * 
	 * <p>Typically, the XML provided is based on the template returned from
	 * a call to <code>prepare</code>.</p>
	 * 
	 * <p>Comments must be associated with an existing resource.  The resource
	 * is required to be denoted in the XML via the 
	 * <code>commentable-id</code> and <code>commentable-type</code>.</p>
	 * 
	 * @param XML Represents a comment.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */
	public static function create(content:XML):BasecampRequest
	{
		// POST /[posts|milestones|todo_items]/#{resource_id}/comments.xml
		// Comments a parent id - no matter the parent type - commentable-id
		var commentableId:String = content.child("commentable-id")[0];
		var commentableType:String = content.child("commentable-type")[0];
		
		SharedOperations.nullEmptyAssert(commentableId, 
		"content did not contain a valid commentable-id", true);
		SharedOperations.nullEmptyAssert(commentableType, 
		"content did not contain a valid commentable-type", true);
		
		
		return new BasecampRequest(commentableType + commentableId + 
		"/comments.xml", URLRequestMethod.POST, TYPE, 
		BasecampOperation.CREATE, content);
	}
		
	/**
	 * Builds a <code>BasecampRequest</code> for deleting a comment.
	 * 
	 * @param id The id of the comment to be deleted.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function destroy(id:String):BasecampRequest
	{
		return SharedOperations.destroy(id, PATH_HELPER, TYPE);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for getting an existing comment
	 * from the store.
	 * 
	 * <p>The XML returned is pre-filled with the existing values of 
	 * the identified comment, and is ready to be resubmitted via the 
	 * <code>update</code> function.</p>
	 * 
	 * @param id The id of the comment to be retrieved.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function edit(id:String):BasecampRequest
	{
		return SharedOperations.edit(id, PATH_HELPER, TYPE);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for reading a list of comments.
	 * 
	 * <p>The list returns the most recent 50 comments.</p>
	 * 
	 * @param resouceId The id of a resource from which comments are to be 
	 * queried.
	 * @param commentableType The type of commentable resource represented by
	 * the <code>resourceId</code>. 
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 * 
	 * @see CategoryFilter
	 */ 
	public static function list(resourceId:String, 
								commentableType:String):BasecampRequest
	{
		SharedOperations.nullEmptyAssert(resourceId, "resourceId");
		SharedOperations.nullEmptyAssert(commentableType, "commentableType");
		
		// GET /[posts|milestones|todo_items]/#{resource_id}/comments.xml
		return new BasecampRequest(
		commentableType + resourceId + "/comments.xml", URLRequestMethod.GET, 
		TYPE, BasecampOperation.LIST);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for retrieving a template for a 
	 * new comment.
	 * 
	 * <p>The template retrieved is used with <code>create</code> to 
	 * submit a new comment.</p>
	 * 
	 * @param resourceId The id of the resource that is to be associated
	 * with the comment.
	 * @param commentableType The type of resource represented by the 
	 * <code>resourceId</code>.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 * 
	 * @see tyemill.summit.CategoryFilter
	 */ 
	public static function prepare(resourceId:String, 
	commentableType:String):BasecampRequest
	{
		// GET /[posts|milestones|todo_items]/#{resource_id}/comments/new.xml
		
		SharedOperations.nullEmptyAssert(resourceId, "resourceId");
		SharedOperations.nullEmptyAssert(commentableType, "commentableType");
		
		var additions:Object = new Object();
		additions["commentable-id"] = resourceId;
		additions["commentable-type"] = commentableType;
		
		return SharedOperations.prepare(commentableType + resourceId + 
			PATH_HELPER + "new.xml", null, null, TYPE, additions);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for reading a single comment.
	 * 
	 * @param id The id of the comment to be read.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */
	public static function read(id:String):BasecampRequest
	{
		return SharedOperations.read(id, PATH_HELPER, TYPE);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for updating an existing 
	 * comment.
	 * 
	 * <p><code>content</code> submitted to <code>update</code> is typically
	 * retrieved from <code>edit</code>.</p>
	 * 
	 * @param content The updated XML content of an existing comment.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>. 
	 */ 
	public static function update(content:XML):BasecampRequest
	{
		return SharedOperations.update(content, PATH_HELPER, TYPE);	
	}
}
}
//////////////////////////////////////////////////////// gmarius@tyemill.com //